package com.atguigu.gmall.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.beans.VisitorStats;
import com.atguigu.gmall.realtime.utils.ClickHouseUtil;
import com.atguigu.gmall.realtime.utils.DateTimeUtil;
import com.atguigu.gmall.realtime.utils.MyKafkaUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple4;
import org.apache.flink.streaming.api.datastream.*;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.Date;

/**
 * Author: Felix
 * Date: 2021/12/4
 * Desc: 访客主题统计
 * 需要启动的进程
 *      zk、kafka、logger、BaseLogApp、UniqueVisitorApp、UserJumpDetailApp、VisitorStatsApp
 */
public class VisitorStatsApp {
    public static void main(String[] args) throws Exception {
        //TODO 1.基本环境准备
        //1.1 设置流处理环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //1.2 设置并行度
        env.setParallelism(4);
        //TODO 2.检查点相关设置(略)
        //TODO 3.从kafka主题中读取数据
        //3.1 声明相关的主题以及消费者组
        String pageLogTopic = "dwd_page_log";
        String uvTopic = "dwm_unique_visitor";
        String ujdTopic = "dwm_user_jump_detail";
        String groupId = "visitor_stats_group";
        //3.2 创建消费者对象
        FlinkKafkaConsumer<String> pageLogKafkaSource = MyKafkaUtil.getKafkaSource(pageLogTopic, groupId);
        FlinkKafkaConsumer<String> uvKafkaSource = MyKafkaUtil.getKafkaSource(uvTopic, groupId);
        FlinkKafkaConsumer<String> ujdKafkaSource = MyKafkaUtil.getKafkaSource(ujdTopic, groupId);
        //3.3 消费数据  封装为流
        DataStreamSource<String> pageLogStrDS = env.addSource(pageLogKafkaSource);
        DataStreamSource<String> uvStrDS = env.addSource(uvKafkaSource);
        DataStreamSource<String> ujdStrDS = env.addSource(ujdKafkaSource);

        //pageLogStrDS.print(">>>");
        //uvStrDS.print("###");
        //ujdStrDS.print("$$$$");

        //TODO 4.对各个流的数据进行类型转换  jsonStr-->VisitorStats实体类对象
        //4.1 pageLog
        SingleOutputStreamOperator<VisitorStats> pageLogStatsDS = pageLogStrDS.map(
                new MapFunction<String, VisitorStats>() {
                    @Override
                    public VisitorStats map(String jsonStr) throws Exception {
                        //将json字符串转换为json对象
                        JSONObject jsonObj = JSON.parseObject(jsonStr);
                        JSONObject commonJsonObj = jsonObj.getJSONObject("common");

                        JSONObject pageJsonObj = jsonObj.getJSONObject("page");
                        VisitorStats visitorStats = new VisitorStats(
                                "",
                                "",
                                commonJsonObj.getString("vc"),
                                commonJsonObj.getString("ch"),
                                commonJsonObj.getString("ar"),
                                commonJsonObj.getString("is_new"),
                                0L,
                                1L,
                                0L,
                                0L,
                                pageJsonObj.getLong("during_time"),
                                jsonObj.getLong("ts")
                        );

                        String lastPageId = pageJsonObj.getString("last_page_id");
                        if (lastPageId == null || lastPageId.length() == 0) {
                            visitorStats.setSv_ct(1L);
                        }
                        return visitorStats;
                    }
                }
        );
        //4.2 uv
        SingleOutputStreamOperator<VisitorStats> uvStatsDS = uvStrDS.map(
                new MapFunction<String, VisitorStats>() {
                    @Override
                    public VisitorStats map(String jsonStr) throws Exception {
                        JSONObject jsonObj = JSON.parseObject(jsonStr);
                        JSONObject commonJsonObj = jsonObj.getJSONObject("common");
                        return new VisitorStats(
                                "",
                                "",
                                commonJsonObj.getString("vc"),
                                commonJsonObj.getString("ch"),
                                commonJsonObj.getString("ar"),
                                commonJsonObj.getString("is_new"),
                                1L, 0L, 0L, 0L, 0L, jsonObj.getLong("ts")
                        );
                    }
                }
        );
        //4.3 ujd
        SingleOutputStreamOperator<VisitorStats> ujdStatDS = ujdStrDS.map(
                new MapFunction<String, VisitorStats>() {
                    @Override
                    public VisitorStats map(String jsonStr) throws Exception {
                        JSONObject jsonObj = JSON.parseObject(jsonStr);
                        JSONObject commonJsonObj = jsonObj.getJSONObject("common");
                        return new VisitorStats(
                                "",
                                "",
                                commonJsonObj.getString("vc"),
                                commonJsonObj.getString("ch"),
                                commonJsonObj.getString("ar"),
                                commonJsonObj.getString("is_new"),
                                0L, 0L, 0L, 1L, 0L, jsonObj.getLong("ts")
                        );
                    }
                }
        );


        //TODO 5.将三条流的数据进行合并
        DataStream<VisitorStats> unionDS = pageLogStatsDS.union(
                uvStatsDS,
                ujdStatDS
        );

        //TODO 6.指定Watermark以及提取事件时间字段
        SingleOutputStreamOperator<VisitorStats> visitorStatsWithWatermarkDS = unionDS.assignTimestampsAndWatermarks(
                WatermarkStrategy
                        .<VisitorStats>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner(
                                new SerializableTimestampAssigner<VisitorStats>() {
                                    @Override
                                    public long extractTimestamp(VisitorStats visitorStats, long recordTimestamp) {
                                        return visitorStats.getTs();
                                    }
                                }
                        )
        );

        //TODO 7.分组  注意：如果按照nid进行分组的话，单位时间内，不会有太多的操作，达不到聚合的效果。所以我们这里按照维度进行分组
        KeyedStream<VisitorStats, Tuple4<String, String, String, String>> keyedDS = visitorStatsWithWatermarkDS.keyBy(
                new KeySelector<VisitorStats, Tuple4<String, String, String, String>>() {
                    @Override
                    public Tuple4<String, String, String, String> getKey(VisitorStats visitorStats) throws Exception {
                        return Tuple4.of(
                                visitorStats.getVc(),
                                visitorStats.getAr(),
                                visitorStats.getCh(),
                                visitorStats.getIs_new()
                        );
                    }
                }
        );

        //TODO 8.开窗
        WindowedStream<VisitorStats, Tuple4<String, String, String, String>, TimeWindow> windowDS
                = keyedDS.window(TumblingEventTimeWindows.of(Time.seconds(10)));

        //TODO 9.聚合计算
        SingleOutputStreamOperator<VisitorStats> reduceDS = windowDS.reduce(
                new ReduceFunction<VisitorStats>() {
                    @Override
                    public VisitorStats reduce(VisitorStats stats1, VisitorStats stats2) throws Exception {
                        stats1.setPv_ct(stats1.getPv_ct() + stats2.getPv_ct());
                        stats1.setUv_ct(stats1.getUv_ct() + stats2.getUv_ct());
                        stats1.setUj_ct(stats1.getUj_ct() + stats2.getUj_ct());
                        stats1.setDur_sum(stats1.getDur_sum() + stats2.getDur_sum());
                        stats1.setSv_ct(stats1.getSv_ct() + stats2.getSv_ct());
                        return stats1;
                    }
                },
                new WindowFunction<VisitorStats, VisitorStats, Tuple4<String, String, String, String>, TimeWindow>() {
                    @Override
                    public void apply(Tuple4<String, String, String, String> tuple4, TimeWindow window, Iterable<VisitorStats> elements, Collector<VisitorStats> out) throws Exception {
                        for (VisitorStats visitorStats : elements) {
                            visitorStats.setStt(DateTimeUtil.toYmdhms(new Date(window.getStart())));
                            visitorStats.setEdt(DateTimeUtil.toYmdhms(new Date(window.getEnd())));
                            visitorStats.setTs(System.currentTimeMillis());
                            out.collect(visitorStats);
                        }
                    }
                }
        );

        reduceDS.print(">>>>>");

        //TODO 10.将聚合的结果写到ClickHouse
        reduceDS.addSink(
                ClickHouseUtil.<VisitorStats>getSinkFunction("insert into visitor_stats_0609 values(?,?,?,?,?,?,?,?,?,?,?,?)")
        );
        env.execute();
    }
}